<?php
namespace Admin\Controller;

class AdvertisementController extends AdminController
{
    public function advertisement()
    {
        $nickname      = I('nickname');
        $map['status'] = array('egt', 0);
        if (is_numeric($nickname)) {
            $map['id|title'] = array(intval($nickname), array('like', '%' . $nickname . '%'), '_multi' => true);
        } else {
            $map['title'] = array('like', '%' . (string) $nickname . '%');
        }

        $list = $this->lists('advertisement', $map);
        $this->assign('_list', $list);
        $this->meta_title = '广告管理';
        $this->display();
    }
    
    public function add()
    {
        if (IS_POST) {
            $advertisement = D('advertisement');
            $res           = $advertisement->create(I('post.'));
            if ($res) {
                if ($advertisement->add($res)) {
                    $this->success('添加成功', U('Advertisement/advertisement'));
                } else {
                    $this->error($advertisement->getError());
                }
            } else {
                $this->error($advertisement->getError());
            }
        } else {
            $this->display();
        }
    }

    public function update()
    {
        $id            = I('get.id');
        $advertisement = D('advertisement');
        $res           = $advertisement->where(array('id' => $id))->setField('active', '1');
        if ($res) {
            $this->success('修改成功');
        } else {
            $this->error($advertisement->getError());
        }
    }

    public function delete()
    {
        $id            = I('get.id');
        $advertisement = D('advertisement');
        $res           = $advertisement->where(array('id' => $id))->delete();
        if ($res) {
            $this->success('删除成功');
        } else {
            $this->error($advertisement->getError());
        }
    }

    public function changeStatus($method=null){
        $id = array_unique((array)I('id',0));
        if( in_array(C('USER_ADMINISTRATOR'), $id)){
            $this->error("不允许对超级管理员执行该操作!");
        }
        $id = is_array($id) ? implode(',',$id) : $id;
        if ( empty($id) ) {
            $this->error('请选择要操作的数据!');
        }
        $map['id'] =   array('in',$id);
        switch ( strtolower($method) ){
            case 'forbiduser':
                $this->forbid('advertisement', $map );
                break;
            case 'resumeuser':
                $this->resume('advertisement', $map );
                break;
            case 'deleteuser':
                $this->delete('advertisement', $map );
                break;
            default:
                $this->error('参数非法');
        }
    }
}
